package com.campus.counseling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.counseling.model.common.exception.ServiceException;
import com.campus.counseling.entity.SysRole;
import com.campus.counseling.entity.SysRoleMenu;
import com.campus.counseling.model.mapper.SysRoleMapper;
import com.campus.counseling.model.mapper.SysRoleMenuMapper;
import com.campus.counseling.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    private final SysRoleMenuMapper roleMenuMapper;

    @Override
    public Page<SysRole> getRolePage(Integer pageNum, Integer pageSize, String roleName) {
        return page(new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<SysRole>()
                        .like(StringUtils.hasText(roleName), SysRole::getRoleName, roleName)
                        .orderByAsc(SysRole::getSort));
    }

    @Override
    public void addRole(SysRole role) {
        if (isRoleKeyExists(role.getRoleKey())) {
            throw new ServiceException("角色标识已存在");
        }
        save(role);
    }

    @Override
    public void updateRole(SysRole role) {
        SysRole existingRole = getById(role.getId());
        if (existingRole == null) {
            throw new ServiceException("角色不存在");
        }
        if (!existingRole.getRoleKey().equals(role.getRoleKey()) && isRoleKeyExists(role.getRoleKey())) {
            throw new ServiceException("角色标识已存在");
        }
        updateById(role);
    }

    @Override
    public void deleteRole(Long roleId) {
        removeById(roleId);
        // 删除角色菜单关联
        roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId));
    }

    @Override
    @Transactional
    public void assignRoleMenu(Long roleId, List<Long> menuIds) {
        // 删除原有关联
        roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId));
        
        // 添加新关联
        List<SysRoleMenu> roleMenus = menuIds.stream()
                .map(menuId -> {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setRoleId(roleId);
                    roleMenu.setMenuId(menuId);
                    return roleMenu;
                }).collect(Collectors.toList());
        
        roleMenus.forEach(roleMenuMapper::insert);
    }

    @Override
    public List<Long> getRoleMenuIds(Long roleId) {
        return roleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId))
                .stream()
                .map(SysRoleMenu::getMenuId)
                .collect(Collectors.toList());
    }

    private boolean isRoleKeyExists(String roleKey) {
        return count(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getRoleKey, roleKey)) > 0;
    }
} 